<?php

namespace app\admin\controller;
use app\common\controller\Backend;
use think\DB;
/**
 *
 *
 * @icon fa fa-circle-o
 */
class Menjin extends Backend
{

  /*门禁*/
  public function index()
  {
      $level=DB::name('menjin')->order('time desc')->paginate(10);
      if($level->toArray())
      {
          foreach($level as $k=>$v)
          {
              $data=array();
              $data=$v;
              if($v['store_id'])
              {
                  $cause['id']=$v['store_id'];
                  $store=DB::name('store')->where($cause)->field('store_name')->find();
                  $data['store_name']=$store['store_name'];
              }else{
                  $data['store_name']="暂未分配";
              }
              if($v['state']==1)
              {
                  $data['state_name']='启用';
              }else{
                  $data['state_name']='禁用';
              }
              $level->offsetSet($k,$data);
          }
      }
      $this->assign('menjin',$level);
      $this->assign('page',$level->render());
      return $this->fetch('menjin/index');
  }
  /*
   * 添加门禁
   */
  public function add()
  {
      if($_POST)
      {
          $array=$this->request->param();
          $array['time']=time();
          $state=DB::name('menjin')->insert($array);
          if($state)
          {
            return  $this->success('添加成功',url('menjin/index'));
          }else{
           return   $this->error('添加失败');
          }
      }else{
          $where['state']=1;
          $store=DB::name('store')->where($where)->select();
          $this->assign('store',$store);
          return $this->fetch('menjin/add');
      }
  }
  /*
   *修改门禁
   * */
  public function detail()
  {
      if($_POST)
      {
          $where['id']=$_POST['id'];
          $date['name']=$_POST['name'];
          $date['store_id']=$_POST['store_id'];
          $date['state']=$_POST['state'];
          $level=DB::name('menjin')->where($where)->update($date);
          return $this->success($level,'menjin/index');
      }else{
          $param = $this->request->param();
          $where['state']=1;
          $store=DB::name('store')->where($where)->select();
          $id=$param['id'];
          $level=DB::name('menjin')->where(array('id'=>$id))->find();
          $this->assign('menjin',$level);
          $this->assign('store',$store);
          return $this->fetch('detail');
      }
  }
  /*删除*/
  public  function delete(){
      $param = $this->request->param();
      $id=$param['id'];
      DB::name('menjin')->where('id',$id)->delete();
      $this->success('删除成功');
  }
}
